package com.zw.cloud.common.aop;

import cn.hutool.json.JSONUtil;
import com.zw.cloud.common.exception.BizException;
import com.zw.cloud.common.utils.DateTimeUtils;
import com.zw.cloud.common.utils.DingTalkUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Slf4j
@Aspect
@Component
public class DingTalkAspect {

    //@AfterThrowing(pointcut = "execution(* com.zw.cloud..*.*(..)) && !execution(* com.zw.cloud..*.*Controller.*(..))",throwing = "ex")
    public void afterThrowing(JoinPoint joinPoint, Exception ex) {

        Signature joinPointSignature = joinPoint.getSignature();
        Class classType = joinPointSignature.getDeclaringType();
        //String className = joinPointSignature.getDeclaringTypeName();
        String methodName = joinPointSignature.getName();
        log.info("[DingTalkAspect][afterthrowing] class is {},method is {},error is ",
                classType,methodName,ex);
        if (ex instanceof BizException) {
            return;
        }
        try {
            String br = " \n\n ";
            String msg = "#### 程序运行出错\n" +
                    "> happenTime : "+ DateTimeUtils.parse2Str(LocalDateTime.now(),"yyyy-MM-dd HH:mm:ss") + br +
                    "> className : "+ classType + br +
                    "> methodName : "+ methodName + br +
                    "> param : "+ JSONUtil.toJsonStr(joinPoint.getArgs()) + br +
                    "> error : "+ ex + br +
                    //"> ![screenshot](https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.jj20.com%2Fup%2Fallimg%2Fmx12%2F0F420115037%2F200F4115037-11.jpg&refer=http%3A%2F%2Fpic.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1638372678&t=004b8405f7b0780a5226dbb99b04c924)\n"  +
                    "> [百度一下](https://www.baidu.com/) \n";
            DingTalkUtils.sendDingTalkMsgWithSign("请注意",msg);

        } catch (Exception e) {
            log.error("[DingTalkAspect][afterthrowing]sendDingTalkMsg error is {},class is {},method is {}",
                    e,classType,methodName);
        }
    }

    /*@AfterThrowing(value = "@annotation(sendDingTalkMsgOnException)",throwing = "ex")
    public void afterthrowing(JoinPoint joinPoint, Exception ex, SendDingTalkMsgOnException sendDingTalkMsgOnException) {
        Signature joinPointSignature = joinPoint.getSignature();
        Class classType = joinPointSignature.getDeclaringType();
        //String className = joinPointSignature.getDeclaringTypeName();
        String methodName = joinPointSignature.getName();
        String token = sendDingTalkMsgOnException.token();
        log.info("[DingTalkAspect][afterthrowing] class is {},method is {},error is {}",
                classType,methodName,ex);
        if (ex instanceof BizException) {
            return;
        }
        try {
            DingTalkUtils.sendDingTalkMsg(token,classType,methodName,joinPoint.getArgs(),ex);
        } catch (Exception e) {
            log.error("[DingTalkAspect][afterthrowing]sendDingTalkMsg error is {},class is {},method is {}",
                    e,classType,methodName);
        }
    }*/

    /*@Around("@annotation(sendDingTalkMsgOnException)")
    public void afterthrowing(ProceedingJoinPoint joinPoint, SendDingTalkMsgOnException sendDingTalkMsgOnException){
        Signature joinPointSignature = joinPoint.getSignature();
        String className = joinPointSignature.getDeclaringTypeName();
        String methodName = joinPointSignature.getName();
        String token = sendDingTalkMsgOnException.token();
        System.out.println(className);
        System.out.println(methodName);
        System.out.println(token);
    }*/
}
